/*====================================================================================
                                                                     
		          O-Ring Production Networks
			        By Demir-Fieler-Xu-Yang
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

/***************************************
/Table 1
****************************************/

use manufsample_2015.dta,det

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

gen size=ln(workers_b)

reg supavgwage_w avgwage_adj_b , vce(cluster s)
estimates store col1
estadd local "FE" "" , replace

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_w avgwage_adj_b size,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

//All firms
use masterdata.dta,clear

keep if year==2015

drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

gen supavgwage_w=avgwage_adj_s*value_share

collapse (mean) avgwage_adj_b  (sum) supavgwage_w ,by(buyer_ID nace4_b nace2_b province_code_b   )

keep if supavgwage_w!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col4
estadd local "FE" "sr" , replace


noi esttab col1 col2 col3 col4, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

/***************************************
/Table 2
****************************************/

use manufsample_2015.dta,det

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgwage_uw avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_cov avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A1
****************************************/

//Panel A

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

//Merge with average worker fixed effects
gen  ID=buyer_ID
destring ID,replace
sort ID year
merge ID year using "wfe2.dta"
keep if _merge==3 
drop _merge ID  
rename w_fe w_fe_b

gen  ID=seller_ID
destring ID,replace
sort ID year
merge ID year using "wfe2.dta"
keep if _merge==3 
drop _merge ID  
rename w_fe w_fe_s

gen supavgskill_w=w_fe_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgskill_uw=mean(w_fe_s)
gen supavgskill_cov=(w_fe_s-supavgskill_uw)*(value_share-1/indegree)

collapse (mean) w_fe_b supavgskill_uw avgwage_adj_b  (sum) supavgskill_w supavgskill_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgskill_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )


reghdfe supavgskill_w w_fe_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgskill_uw w_fe_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgskill_cov w_fe_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


//Panel B and Panel C

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

//Merge with other skill measures
gen  ID=buyer_ID
destring ID,replace
sort ID 
merge ID  using "skills_other.dta"
keep if _merge==3 
drop _merge ID  
replace numocc=ln(numocc)
for X in any skills_NR  numocc :rename X X_b

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID  using "skills_other.dta"
keep if _merge==3 
drop _merge ID  
replace numocc=ln(numocc)
for X in any skills_NR  numocc :rename X X_s

bys buyer_ID ,sort: gen indegree=_N

foreach y of varlist skills_NR  numocc {
	gen supavg`y'_w=`y'_s*value_share
bys buyer_ID ,sort: egen supavg`y'_uw=mean(`y'_s)
gen supavg`y'_cov=(`y'_s-supavg`y'_uw)*(value_share-1/indegree)

}


collapse (mean) skills_NR_b  numocc_b supavgskills_NR_uw supavgnumocc_uw avgwage_adj_b  (sum) supavgskills_NR_w supavgnumocc_w supavgskills_NR_cov supavgnumocc_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgskills_NR_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )


reghdfe supavgskills_NR_w skills_NR_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgskills_NR_uw skills_NR_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgskills_NR_cov skills_NR_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

reghdfe supavgnumocc_w numocc_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgnumocc_uw numocc_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgnumocc_cov numocc_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


//Panel D
*Construct KSW quality measure
use "ihracat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet value
rename miktar quantity

keep if year==2015

gen hs8=floor(HS12/10000)

collapse (sum) value quantity,by(ID ulke hs8)

gen sigma=5
gen lnuv=ln(value/quantity)
gen dep=ln(quantity)+sigma*lnuv

egen c=group(ulke)
egen p=group(hs8)

reghdfe dep ,absorb(c p) residuals(q_r)

gen lnq= q_r/(sigma-1)

collapse (mean) lnq ,by(ID)

destring ID,replace
sort ID 
save temp3.dta,replace


use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

//Merge with KSW quality measure
gen  ID=buyer_ID
destring ID,replace
sort ID 
merge ID  using "temp3.dta"
keep if _merge==3 
drop _merge ID  
rename lnq lnq_b

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID  using "temp3.dta"
keep if _merge==3 
drop _merge ID  
rename lnq lnq_s
capture erase temp3.dta

gen supavgq_w=lnq_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgq_uw=mean(lnq_s)
gen supavgq_cov=(lnq_s-supavgq_uw)*(value_share-1/indegree)

collapse (mean) lnq_b supavgq_uw avgwage_adj_b  (sum) supavgq_w supavgq_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgq_uw!=.

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )


reghdfe supavgq_w lnq_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgq_uw lnq_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgq_cov lnq_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A2
****************************************/

// Panel A

use manufsample_2015.dta,det

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )
egen d=group(province_code_b sub_province_b)

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr d) vce(cluster s)
estimates store col1
estadd local "FE" "sr,d" , replace

reghdfe  supavgwage_uw avgwage_adj_b ,absorb(sr d) vce(cluster s)
estimates store col2
estadd local "FE" "sr,d" , replace

reghdfe  supavgwage_cov avgwage_adj_b ,absorb(sr d) vce(cluster s)
estimates store col3
estadd local "FE" "sr,d" , replace

// Panel B

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

drop if province_code_b==province_code_s // Drop business partners located in the same province

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

gen supavgwage_w=avgwage_adj_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgwage_uw=mean(avgwage_adj_s)
gen supavgwage_cov=(avgwage_adj_s-supavgwage_uw)*(value_share-1/indegree)

collapse (mean) avgwage_adj_b supavgwage_uw  workers_b wagebill_b sales_b mktshare_b indegree (sum) supavgwage_w supavgwage_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgwage_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgwage_uw avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_cov avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

// Panel C

// Identify multi-establishment firms

use "data\isyeri_full.dta",clear
keep if year==2015

collapse (count) sgkno,by(ID)
rename sgkno numest
sort ID
save temp3.dta,replace

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

*Drop multi-establishment buyers and suppliers
gen  ID=buyer_ID
destring ID,replace
sort ID 
merge ID  using "temp3.dta"
keep if numest==1
drop _merge ID  numest

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID  using "temp3.dta"
keep if numest==1
drop _merge ID  numest

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

gen supavgwage_w=avgwage_adj_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgwage_uw=mean(avgwage_adj_s)
gen supavgwage_cov=(avgwage_adj_s-supavgwage_uw)*(value_share-1/indegree)

collapse (mean) avgwage_adj_b supavgwage_uw  workers_b wagebill_b sales_b mktshare_b indegree (sum) supavgwage_w supavgwage_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgwage_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgwage_uw avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_cov avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A3
****************************************/


use manufsample_2015.dta,det

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

gen exporter=(exports!=0)
gen importer=(imports!=0)
gen lnmktshare_b=ln(mktshare_b)
gen lnindegree=ln(indegree)
gen lnoutdegree=ln(outdegree)

reghdfe supavgwage_w avgwage_adj_b lnmktshare_b foreign exporter importer  lnoutdegree lnindegree,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgwage_uw avgwage_adj_b lnmktshare_b foreign exporter importer  lnoutdegree lnindegree,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_cov avgwage_adj_b lnmktshare_b foreign exporter importer  lnoutdegree lnindegree,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A4
****************************************/

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

//Drop buyers and suppliers with foreign ownership
gen  ID=buyer_ID
sort ID 
merge ID using "ForeignOwn_Snapshot.dta"
drop if foreign==1 
drop _merge ID  foreign

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID using "ForeignOwn_Snapshot.dta"
drop if foreign==1 
drop _merge ID  foreign

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

gen supavgwage_w=avgwage_adj_s*value_share

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgwage_uw=mean(avgwage_adj_s)
gen supavgwage_cov=(avgwage_adj_s-supavgwage_uw)*(value_share-1/indegree)

collapse (mean) avgwage_adj_b supavgwage_uw  workers_b wagebill_b sales_b mktshare_b indegree (sum) supavgwage_w supavgwage_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

keep if supavgwage_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

reghdfe supavgwage_w avgwage_adj_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgwage_uw avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgwage_cov avgwage_adj_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

/***************************************
/Table A5
****************************************/


use masterdata.dta,clear

keep if year==2015

drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b
gen value_manuf=value if nace2_s <34
bys buyer_ID,sort: egen total_value_manuf_b=sum(value_manuf)
gen value_share_manuf=value_manuf/total_value_manuf_b

bys seller_ID ,sort: gen outdegree=_N
gen buyer_manuf=(nace2_b <34)
bys seller_ID,sort: egen outdegree_manuf=sum(buyer_manuf)

gen mktshare_s_manuf=mktshare_s if nace2_s<34

bys buyer_ID ,sort: gen indegree=_N
gen seller_manuf=(nace2_s<34)
bys buyer_ID,sort: egen indegree_manuf=sum(seller_manuf)

foreach y of varlist avgwage_adj_b mktshare_s mktshare_s_manuf mktshare_b outdegree outdegree_manuf {
	replace `y'=ln(`y')
}

 foreach y of varlist mktshare_s outdegree {	
gen supavg`y'_w=`y'*value_share
bys buyer_ID ,sort: egen supavg`y'_uw=mean(`y')
gen supavg`y'_cov=(`y'-supavg`y'_uw)*(value_share-1/indegree)

 }

 foreach y of varlist mktshare_s_manuf outdegree_manuf {	
gen supavg`y'_w=`y'*value_share_manuf
bys buyer_ID ,sort: egen supavg`y'_uw=mean(`y')
gen supavg`y'_cov=(`y'-supavg`y'_uw)*(value_share_manuf-1/indegree_manuf)

 }

collapse (mean) avgwage_adj_b supavgmktshare_s_uw supavgoutdegree_uw supavgmktshare_s_manuf_uw supavgoutdegree_manuf_uw mktshare_b indegree indegree_manuf (sum) supavgmktshare_s_w supavgoutdegree_w supavgmktshare_s_manuf_w supavgoutdegree_manuf_w supavgmktshare_s_cov supavgoutdegree_cov supavgmktshare_s_manuf_cov supavgoutdegree_manuf_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

gen M=(nace2_b<34)
bys M,sort: egen p1=pctile(avgwage_adj_b),p(1)
bys M,sort: egen p99=pctile(avgwage_adj_b),p(99)

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )

replace indegree=ln(indegree)
replace indegree_manuf=ln(indegree_manuf)

//Panel A (weighted average)
reghdfe supavgmktshare_s_manuf_w mktshare_b if nace2_b<34 & avgwage_adj_b>p1 & avgwage_adj_b<p99  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe supavgmktshare_s_w mktshare_b  if avgwage_adj_b>p1 & avgwage_adj_b<p99,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe supavgoutdegree_manuf_w indegree_manuf if nace2_b<34 & avgwage_adj_b>p1 & avgwage_adj_b<p99 ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

reghdfe supavgoutdegree_w indegree  if avgwage_adj_b>p1 & avgwage_adj_b<p99,absorb(sr) vce(cluster s)
estimates store col4
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3 col4, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


//Panel B (unweighted average)
reghdfe supavgmktshare_s_manuf_uw mktshare_b if nace2_b<34 & avgwage_adj_b>p1 & avgwage_adj_b<p99 ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe supavgmktshare_s_uw mktshare_b  if avgwage_adj_b>p1 & avgwage_adj_b<p99,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe supavgoutdegree_manuf_uw indegree_manuf if nace2_b<34 & avgwage_adj_b>p1 & avgwage_adj_b<p99 ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

reghdfe supavgoutdegree_uw indegree if avgwage_adj_b>p1 & avgwage_adj_b<p99 ,absorb(sr) vce(cluster s)
estimates store col4
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3 col4 , replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A7 (Middle and bottom panels)
****************************************/

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

//Merge with average worker fixed effects
gen  ID=buyer_ID
destring ID,replace
sort ID 
merge ID  using "reswage_occfe.dta"
keep if _merge==3 
drop _merge ID  
for X in any reswages occ_fe_w: rename X X_b

gen  ID=seller_ID
destring ID,replace
sort ID 
merge ID  using "reswage_occfe.dta"
keep if _merge==3 
drop _merge ID  
for X in any reswages occ_fe_w: rename X X_s

 foreach y of varlist reswages occ_fe_w {	
gen supavg`y'_w=`y'_s*value_share
bys buyer_ID ,sort: egen supavg`y'_uw=mean(`y'_s)
gen supavg`y'_cov=(`y'_s-supavg`y'_uw)*(value_share-1/indegree)

 }


collapse (mean) reswages_b occ_fe_w_b supavgreswages_uw supavgocc_fe_w_uw avgwage_adj_b  (sum) supavgreswages_w supavgocc_fe_w_w supavgreswages_cov supavgocc_fe_w_cov,by(buyer_ID nace4_b nace2_b province_code_b sub_province_b  )

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_b province_code_b )
egen s=group(nace4_b  )


reghdfe supavgreswages_w reswages_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgreswages_uw reswages_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgreswages_cov reswages_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

reghdfe supavgocc_fe_w_w occ_fe_w_b  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  supavgocc_fe_w_uw occ_fe_w_b ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  supavgocc_fe_w_cov occ_fe_w_b ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Table A8 and Panel B of Table A9
****************************************/

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys seller_ID,sort: egen total_value_s=sum(value)
gen value_share=value/total_value_s

gen buyeravgwage_w=avgwage_adj_b*value_share

bys seller_ID ,sort: gen outdegree=_N
bys seller_ID ,sort: egen buyeravgwage_uw=mean(avgwage_adj_b)
gen buyeravgwage_cov=(avgwage_adj_b-buyeravgwage_uw)*(value_share-1/outdegree)

forvalues i=10(10)90 {
bys seller_ID ,sort: egen buyeravgwage_p`i'th=pctile(avgwage_adj_b),p(`i')
}


collapse (mean) avgwage_adj_s buyeravgwage_uw  outdegree buyeravgwage_p* (sum) buyeravgwage_w buyeravgwage_cov,by(seller_ID nace4_s nace2_s province_code_s)

keep if buyeravgwage_uw!=.

su avgwage_adj_s,det
keep if avgwage_adj_s>r(p1) & avgwage_adj_s<r(p99)

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s )

//Table A8
reghdfe buyeravgwage_w avgwage_adj_s  ,absorb(sr) vce(cluster s)
estimates store col1
estadd local "FE" "sr" , replace

reghdfe  buyeravgwage_uw avgwage_adj_s ,absorb(sr) vce(cluster s)
estimates store col2
estadd local "FE" "sr" , replace

reghdfe  buyeravgwage_cov avgwage_adj_s ,absorb(sr) vce(cluster s)
estimates store col3
estadd local "FE" "sr" , replace

noi esttab col1 col2 col3, replace compress width(1\hsize)  se  s(N r2 FE clustvar)

//Panel B of Table A9

forvalues i=10(10)90 {
	reghdfe buyeravgwage_p`i'th avgwage_adj_s if outdegree>=10 ,absorb(sr) vce(cluster s)
estimates store col`i'
estadd local "FE" "sr" , replace
}

noi esttab col1 col2 col3 col4 col5 col6 col7 col8 col9 col10, replace compress width(1\hsize)  se  s(N r2 FE clustvar)


/***************************************
/Panel A of Table A9
****************************************/

use masterdata.dta,clear

keep if year==2015

keep if nace2_b <34 // Keep manufacturing buyers
keep if nace2_s <34 // Keep manufacturing suppliers
drop if avgwage_adj_b==0 | avgwage_adj_b==.
drop if avgwage_adj_s==0 | avgwage_adj_s==.

for X in any avgwage_adj_s avgwage_adj_b: replace X=ln(X) 

bys buyer_ID,sort: egen total_value_b=sum(value)
gen value_share=value/total_value_b

bys buyer_ID ,sort: gen indegree=_N
bys buyer_ID ,sort: egen supavgwage_uw=mean(avgwage_adj_s)

forvalues i=10(10)90 {
bys buyer_ID ,sort: egen supavgwage_p`i'th=pctile(avgwage_adj_s),p(`i')
}

collapse (mean) avgwage_adj_b supavgwage_uw  indegree supavgwage_p*,by(buyer_ID nace4_b nace2_b province_code_b)

keep if supavgwage_uw!=.

su avgwage_adj_b,det
keep if avgwage_adj_b>r(p1) & avgwage_adj_b<r(p99)

egen sr=group(nace4_s province_code_s )
egen s=group(nace4_s )

forvalues i=10(10)90 {
	reghdfe supavgwage_p`i'th avgwage_adj_b if indegree>=10 ,absorb(sr) vce(cluster s)
estimates store col`i'
estadd local "FE" "sr" , replace
}

noi esttab col1 col2 col3 col4 col5 col6 col7 col8 col9 col10, replace compress width(1\hsize)  se  s(N r2 FE clustvar)
